package ctm.javacode;

import java.io.* ;
import java.sql.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/readHistory.do")

public class readHistory extends HttpServlet {
	dbBean db;
	ResultSet rs;
	String sqlQuery, id, time;
	PrintWriter out;
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		db = new dbBean();
		db.setDataSource("jdbc/ctmpllab");
		db.Connected();
		
		out = response.getWriter();//Write the response back to the browser
		
		String type = request.getParameter("type");			
		
		switch (type) {
		case "printdata":			
			id = request.getParameter("id");		
			sqlQuery = "SELECT * FROM `history` where id='" + id + "' order by num desc";
			System.out.println(sqlQuery);
			try {
				rs = db.ExcuteQuery(sqlQuery);
				boolean nodata = true;
				while (rs.next()) {
					if (rs.getString("save").equals("T")) {
						proSuggest();
						nodata = false;
						break;
					}
				}				
				if (nodata == true) { // 確認是否有資料
					out.print("nodata|資料庫無此資料");
				} 					
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			break;
			
		case "suggestHistory":
			String time = request.getParameter("time");
			id = request.getParameter("personId");
			sqlQuery = "SELECT * FROM `history` where id='" + id + "' && time='"+time+"'";
			
			try {
				rs = db.ExcuteQuery(sqlQuery);
				if (rs.first()) // 確認是否有資料
					proSuggest();
				else
					out.print("nodata|資料庫無此資料");
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			break;

		default:
			break;
		}
		
	}

	private void proSuggest() {
		// TODO Auto-generated method stub
		try {			
				//radio type
				out.print(rs.getString("sex") + "|");
				out.print(rs.getString("weightType") + "|");
				out.print(rs.getString("work") + "|");
				out.print(rs.getString("act") + "|");
				//select type
				out.print(rs.getString("nourish_list") + "|");
				//text type
				out.print("#age:" + rs.getString("age") + "|");
				out.print("#height:" + rs.getString("height") + "|");
				out.print("#weight:" + rs.getString("weight") + "|");
				out.print("#pressure:" + rs.getString("press") + "|");
				out.print("#g_kgBW:" + rs.getString("g_kgBW") + "|");
				out.print("#proportion_fat:" + rs.getString("fat_rate") + "|");
				
				out.print("#milk1:" + rs.getString("milk1") + "|");
				out.print("#milk2:" + rs.getString("milk2") + "|");
				out.print("#milk3:" + rs.getString("milk3") + "|");
				out.print("#milk4:" + rs.getString("milk4") + "|");
				out.print("#milk5:" + rs.getString("milk5") + "|");
				out.print("#milk6:" + rs.getString("milk6") + "|");
				
				out.print("#low_milk1:" + rs.getString("low_milk1") + "|");
				out.print("#low_milk2:" + rs.getString("low_milk2") + "|");
				out.print("#low_milk3:" + rs.getString("low_milk3") + "|");
				out.print("#low_milk4:" + rs.getString("low_milk4") + "|");
				out.print("#low_milk5:" + rs.getString("low_milk5") + "|");
				out.print("#low_milk6:" + rs.getString("low_milk6") + "|");
				
				out.print("#high_milk1:" + rs.getString("high_milk1") + "|");
				out.print("#high_milk2:" + rs.getString("high_milk2") + "|");
				out.print("#high_milk3:" + rs.getString("high_milk3") + "|");
				out.print("#high_milk4:" + rs.getString("high_milk4") + "|");
				out.print("#high_milk5:" + rs.getString("high_milk5") + "|");
				out.print("#high_milk6:" + rs.getString("high_milk6") + "|");
						
				out.print("#veg1:" + rs.getString("veg1") + "|");
				out.print("#veg2:" + rs.getString("veg2") + "|");
				out.print("#veg3:" + rs.getString("veg3") + "|");
				out.print("#veg4:" + rs.getString("veg4") + "|");
				out.print("#veg5:" + rs.getString("veg5") + "|");
				out.print("#veg6:" + rs.getString("veg6") + "|");

				out.print("#fruit1:" + rs.getString("fruit1") + "|");
				out.print("#fruit2:" + rs.getString("fruit2") + "|");
				out.print("#fruit3:" + rs.getString("fruit3") + "|");
				out.print("#fruit4:" + rs.getString("fruit4") + "|");
				out.print("#fruit5:" + rs.getString("fruit5") + "|");
				out.print("#fruit6:" + rs.getString("fruit6") + "|");
				
				out.print("#grain1:" + rs.getString("grain1") + "|");
				out.print("#grain2:" + rs.getString("grain2") + "|");
				out.print("#grain3:" + rs.getString("grain3") + "|");
				out.print("#grain4:" + rs.getString("grain4") + "|");
				out.print("#grain5:" + rs.getString("grain5") + "|");
				out.print("#grain6:" + rs.getString("grain6") + "|");
				
				out.print("#low_meat1:" + rs.getString("low_meat1") + "|");
				out.print("#low_meat2:" + rs.getString("low_meat2") + "|");
				out.print("#low_meat3:" + rs.getString("low_meat3") + "|");
				out.print("#low_meat4:" + rs.getString("low_meat4") + "|");
				out.print("#low_meat5:" + rs.getString("low_meat5") + "|");
				out.print("#low_meat6:" + rs.getString("low_meat6") + "|");
				
				out.print("#meat1:" + rs.getString("meat1") + "|");
				out.print("#meat2:" + rs.getString("meat2") + "|");
				out.print("#meat3:" + rs.getString("meat3") + "|");
				out.print("#meat4:" + rs.getString("meat4") + "|");
				out.print("#meat5:" + rs.getString("meat5") + "|");
				out.print("#meat6:" + rs.getString("meat6") + "|");
				
				out.print("#high_meat1:" + rs.getString("high_meat1") + "|");
				out.print("#high_meat2:" + rs.getString("high_meat2") + "|");
				out.print("#high_meat3:" + rs.getString("high_meat3") + "|");
				out.print("#high_meat4:" + rs.getString("high_meat4") + "|");
				out.print("#high_meat5:" + rs.getString("high_meat5") + "|");
				out.print("#high_meat6:" + rs.getString("high_meat6") + "|");
				
				out.print("#fat1:" + rs.getString("fat1") + "|");
				out.print("#fat2:" + rs.getString("fat2") + "|");
				out.print("#fat3:" + rs.getString("fat3") + "|");
				out.print("#fat4:" + rs.getString("fat4") + "|");
				out.print("#fat5:" + rs.getString("fat5") + "|");
				out.print("#fat6:" + rs.getString("fat6") + "|");
				
				out.print("#quantity_Milk:" + rs.getString("quantity_Milk") + "|");
				out.print("#quantity_Veg:" + rs.getString("quantity_Veg") + "|");
				out.print("#quantity_Fruit:" + rs.getString("quantity_Fruit") + "|");
				
				out.print("#type1:" + rs.getString("type1") + "|");
				out.print("#type2:" + rs.getString("type2") + "|");
				out.print("#type3:" + rs.getString("type3") + "|");
				out.print("#type4:" + rs.getString("type4") + "|");
				out.print("#type5:" + rs.getString("type5") + "|");
				out.print("#type6:" + rs.getString("type6") + "|");
				out.print("#type7:" + rs.getString("type7") + "|");
				out.print("#type8:" + rs.getString("type8") + "|");
				out.print("#type9:" + rs.getString("type9") + "|");
				out.print("#type10:" + rs.getString("type10") + "|");
				out.print("#type11:" + rs.getString("type11") + "|");
				out.print("#type12:" + rs.getString("type12") + "|");
				out.print("#type13:" + rs.getString("type13") + "|");
				out.print("#type14:" + rs.getString("type14") + "|");
				out.print("#type15:" + rs.getString("type15") + "|");
				out.print("#type16:" + rs.getString("type16") + "|");
				out.print("#type17:" + rs.getString("type17") + "|");
				
				out.print("#proportion_breakfast:" + rs.getString("breakfast") + "|");
				out.print("#proportion_lunch:" + rs.getString("lunch") + "|");
				out.print("#proportion_dinner:" + rs.getString("dinner") + "|");	
				out.print("#save:" + rs.getString("save") + "|");

				out.print("#breakfast_dessert_Milk:" + rs.getString("assign1") + "|");
				out.print("#lunch_dessert_Milk:" + rs.getString("assign2") + "|");
				out.print("#dinner_dessert_Milk:" + rs.getString("assign3") + "|");
				out.print("#breakfast_dessert_Veg:" + rs.getString("assign4") + "|");
				out.print("#lunch_dessert_Veg:" + rs.getString("assign5") + "|");
				out.print("#dinner_dessert_Veg:" + rs.getString("assign6") + "|");
				out.print("#breakfast_dessert_Fruit:" + rs.getString("assign7") + "|");
				out.print("#lunch_dessert_Fruit:" + rs.getString("assign8") + "|");
				out.print("#dinner_dessert_Fruit:" + rs.getString("assign9") + "|");
				out.print("#breakfast_dessert_Grain:" + rs.getString("assign10") + "|");
				out.print("#lunch_dessert_Grain:" + rs.getString("assign11") + "|");
				out.print("#dinner_dessert_Grain:" + rs.getString("assign12") + "|");
				out.print("#breakfast_dessert_Meat:" + rs.getString("assign13") + "|");
				out.print("#lunch_dessert_Meat:" + rs.getString("assign14") + "|");
				out.print("#dinner_dessert_Meat:" + rs.getString("assign15") + "|");
				out.print("#breakfast_dessert_Fat:" + rs.getString("assign16") + "|");
				out.print("#lunch_dessert_Fat:" + rs.getString("assign17") + "|");
				out.print("#dinner_dessert_Fat:" + rs.getString("assign18") + "|");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}